package com.rainfull.ui;

import java.util.HashMap;

import com.rainfull.core.RNetworkProcess;
import com.rainfull.core.RUserInfo;
import com.rainfull.core.WifiHelper;
import com.rainfull.ui.R;

import android.app.Activity;
import android.net.wifi.ScanResult;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.ProgressDialog;
import android.content.Intent;

public class RUIAccount extends Activity implements OnClickListener
{  
	private main m_mainActive = null;
	private String m_strTip = "";
	private ProgressDialog m_progressDialog = null;
	private boolean m_DlgActive = false;
	private boolean m_connectSvr = false;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_login);        
        init();
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        return false;
    }
    public void init()
    {
    	m_mainActive = (main)this.getParent();
    	//登陆接口
    	Button buttonLogin = (Button) findViewById(R.id.account_button_login);
    	buttonLogin.setOnClickListener(this);
    	//注册接口
    	Button buttonReg = (Button) findViewById(R.id.account_button_reg);
    	buttonReg.setOnClickListener(this);
    	
    	final EditText editUserName =(EditText)findViewById(R.id.account_tex_user_name);	
		RUserInfo userInfo = m_mainActive.GetUserInfoFromStorage();
		editUserName.setText(userInfo.getUserName());
		if (userInfo.getToken() != null && userInfo.getToken().length() > 0)
		{
			HashMap<String, Object> mapRegInfo = new HashMap<String, Object>();
			mapRegInfo.put("token", userInfo.getToken());
			mapRegInfo.put("type", 3);
			m_strTip = "正在登陆,请稍后...";
			//m_progressDialog = ProgressDialog.show(RUIAccount.this, "","正在登陆...", true, true);
			m_connectSvr = true;
			new RUIRegLogin().execute(mapRegInfo);
		}
    }    
    @Override
    public void onPause()
    {
    	super.onPause();
    	m_DlgActive = false;
    }
    
    public void onResume()
    {
    	super.onResume();
    	m_DlgActive = true;
    	if (m_connectSvr == true)
    		m_progressDialog = ProgressDialog.show(RUIAccount.this, "", m_strTip, true);
    }
	public void onClick(View v) 
	{
		int nId = v.getId();
		switch(nId)
		{
			case R.id.account_button_reg:
			{
				Intent intent = new Intent(RUIAccount.this, RUIAccountReg.class);        		
				startActivityForResult(intent, 2);
			}
				break;
			case R.id.account_button_login:			
			{								
				final EditText editUserName =(EditText)findViewById(R.id.account_tex_user_name);
				m_mainActive.showKeyBoard(null, false);
				final EditText editPwd =(EditText)findViewById(R.id.account_tex_pwd);
				HashMap<String, Object> mapRegInfo = new HashMap<String, Object>();
				if (editUserName.getText().toString().length() == 0 ||
						editPwd.getText().toString().length() == 0)
				{
					Toast.makeText(RUIAccount.this, "请输入账号和密码!", Toast.LENGTH_LONG).show();
					return;
				}
				mapRegInfo.put("userName", editUserName.getText().toString());
				mapRegInfo.put("pwd", editPwd.getText().toString());
				mapRegInfo.put("type", 1);
				m_strTip = "正在登陆，请稍后...";
				m_connectSvr = true;
				if (m_DlgActive == true)
					m_progressDialog = ProgressDialog.show(RUIAccount.this, "", m_strTip, true);				
				new RUIRegLogin().execute(mapRegInfo);
				break;
			}
		}		
	}
	public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {  		 		
  		if (resultCode == RESULT_OK)
  		{
  			m_mainActive.m_userInfo.seLeftCount(data.getIntExtra("left_count", 0));
  			m_mainActive.m_userInfo.setScore(data.getIntExtra("score", 0));
  			m_mainActive.m_userInfo.setToken(data.getStringExtra("token"));
  			m_mainActive.m_userInfo.setUserName(data.getStringExtra("user_name"));  			
			m_mainActive.saveUserInfo();			
  			m_mainActive.changeTabView(RUIAccount.class, RUIAccountInfo.class);
  		}		  	
    }
    //登陆注册接口
	public class RUIRegLogin extends AsyncTask
	{
		@Override
		protected Object doInBackground(Object... param) 
		{
			HashMap<String, Object> mapRegInfo = (HashMap<String, Object>)param[0];			   
			
			int nType = Integer.parseInt(mapRegInfo.get("type").toString());
			RUserInfo userInfo = null;
			if (nType == 1)	//登陆
			{
				String strUserName = mapRegInfo.get("userName").toString();
				String strPwd = mapRegInfo.get("pwd").toString();
				userInfo = RNetworkProcess.userLogin(strUserName, strPwd);
			}
			else if (nType == 3)	//登陆
			{
				String strToken = mapRegInfo.get("token").toString();
				userInfo = RNetworkProcess.userLoginEx(strToken);
			}			
			m_connectSvr = false;
			mapRegInfo.put("userInfo", userInfo);			
			return mapRegInfo;
		}
		protected void onPostExecute(Object result) 
		{  
			if (m_progressDialog != null)
				m_progressDialog.dismiss();
			HashMap<String, Object> mapRegInfo = (HashMap<String, Object>)result;			   
			RUserInfo userInfo = (RUserInfo)mapRegInfo.get("userInfo");		 
			if (userInfo.getUserName().length() == 0)
			{
				int nType = Integer.parseInt(mapRegInfo.get("type").toString());
				Toast.makeText(RUIAccount.this, "登陆失败，请重试!", Toast.LENGTH_LONG).show();				
			}
			else
			{
				m_mainActive.m_userInfo = userInfo;				
				m_mainActive.saveUserInfo();
				m_mainActive.changeTabView(RUIAccount.class, RUIAccountInfo.class);				
			}
		}
	}
}
